package com.lz.springboot.rabbit;

import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;

@Component
@EnableApolloConfig({ "Java.rabbit" })
@PropertySource(value = "classpath:rabbit.properties", ignoreResourceNotFound = true)
public class ApolloConfigRabbit {

	private static final Logger LOGGER = LoggerFactory.getLogger(ApolloConfigRabbit.class);
	@ApolloConfig("Java.rabbit")
	private Config config;

	@Value("${spring.rabbitmq.host:127.0.0.1}")
	private String host;
	@Value("${spring.rabbitmq.port:5672}")
	private Integer port;
	@Value("${spring.rabbitmq.username:rabbitmq}")
	private String username;
	@Value("${spring.rabbitmq.password:rabbitmq}")
	private String password;
	@Value("${spring.rabbitmq.virtual-host:lzmh}")
	private String virtualHost;
	@Value("${spring.rabbitmq.publisher-confirms:false}")
	private Boolean publisherConfirms;
	@Value("${spring.rabbitmq.publisher-returns:false}")
	private Boolean publisherReturns;
	

	@ApolloConfigChangeListener("Java.rabbit")
	private void anotherOnChange(ConfigChangeEvent changeEvent) {
		Set<String> configNames = config.getPropertyNames();
		for (String configName : configNames) {
			LOGGER.info("configName=[{}] configValue=[{}]", configName, config.getProperty(configName, ""));
		}
	}


	@Override
	public String toString() {
		return "ApolloConfigRabbit [host=" + host + ", port=" + port + ", username=" + username + ", password="
				+ password + ", virtualHost=" + virtualHost + ", publisherConfirms=" + publisherConfirms
				+ ", publisherReturns=" + publisherReturns + "]";
	}
	
}